package com.travelation;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.travelation.model.Post;

public class PostList extends ListActivity {

	private static final int MENU_REPARSE = 0;

	private TravelationApp app;
	private List<Post> items;
	private PostsAdapter postsAdapter;
	private ArrayAdapter<String> spinnerAdapter;
	private Spinner sectionSpinner;
	private int currentSelectedSection;
	private ProgressDialog progressDialog;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.postlist);

		progressDialog = new ProgressDialog(this);
		progressDialog.setCancelable(false);
		progressDialog
				.setMessage(getString(R.string.postlist_retrieving_data));

		// Use Application object for app wide state
		app = (TravelationApp) getApplication();

		// construct Adapter with empty items collection to start
		items = new ArrayList<Post>();
		postsAdapter = new PostsAdapter(items);

		// ListView adapter (this class extends ListActivity)
		setListAdapter(postsAdapter);

		// get Sections list from application (parsing feed if necessary)
		if (app.getPostList().isEmpty()) {
			if (app.connectionPresent()) {
				new ParseFeedTask().execute();
			} else {
				Toast.makeText(this,
						getString(R.string.postlist_network_unavailable),
						Toast.LENGTH_LONG).show();
			}
		} else {
			resetListItems(app.getPostList());
		}

		// Spinner for choosing a Section
		sectionSpinner = (Spinner) findViewById(R.id.section_spinner);
		spinnerAdapter = new ArrayAdapter<String>(PostList.this,
				android.R.layout.simple_spinner_item, new String[] { "all",
						"history" });
		spinnerAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		sectionSpinner.setAdapter(spinnerAdapter);
		sectionSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> parentView,
					View selectedItemView, int position, long id) {
				if (currentSelectedSection != position) {
					currentSelectedSection = position;
					resetListItems(app.getPostList());
				}
			}
			@Override
			public void onNothingSelected(AdapterView<?> parentView) {
				// do nothing
			}
		});
	}

	private void resetListItems(List<Post> newItems) {
		items.clear();
		items.addAll(newItems);
		postsAdapter.notifyDataSetChanged();
	}

	@Override
	protected void onListItemClick(ListView listView, View view, int position,
			long id) {
		view.setBackgroundColor(android.R.color.background_light);
		app.setCurrentItem(app.getPostList().get(position));
		Intent postDetails = new Intent(PostList.this, PostDetails.class);
		startActivity(postDetails);
	}

	@Override
	public void onPause() {
		if (progressDialog.isShowing()) {
			progressDialog.dismiss();
		}
		super.onPause();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, PostList.MENU_REPARSE, 0, R.string.postlist_reparse_menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case MENU_REPARSE:
			if (app.connectionPresent()) {
				new ParseFeedTask().execute();
			} else {
				Toast.makeText(this,
						getString(R.string.postlist_network_unavailable),
						Toast.LENGTH_LONG).show();
			}
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	// Use a custom Adapter to control the layout and views
	private class PostsAdapter extends ArrayAdapter<Post> {

		public PostsAdapter(List<Post> items) {
			super(PostList.this, R.layout.list_item, items);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			if (convertView == null) {
				LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				convertView = inflater.inflate(R.layout.list_item, parent,
						false);
			}

			// use ViewHolder here to prevent multiple calls to findViewById (if
			// you have a large collection)
			TextView text = (TextView) convertView
					.findViewById(R.id.post_title);
			ImageView image = (ImageView) convertView
					.findViewById(R.id.post_img);
			image.setImageBitmap(BitmapFactory.decodeResource(getResources(),
					R.drawable.ddicon));

			Post item = getItem(position);

			if (item != null) {
				text.setText(item.getTitle());
				Bitmap bitmap = app.getImageCache().get(item.getItemId());
				if (bitmap != null) {
					image.setImageBitmap(bitmap);
				} else {
					// put item ID on image as TAG for use in task
					image.setTag(item.getItemId());
					// separate thread/via task, for retrieving each image
					// (note that this is brittle as is, should stop all threads
					// in onPause)
					new RetrieveImageTask(image).execute(item.getSmallPicUrl());
				}
			}

			return convertView;
		}
	}

	// Use an AsyncTask<Params, Progress, Result> to easily perform tasks off of
	// the UI Thread
	private class ParseFeedTask extends AsyncTask<Void, Void, List<Post>> {

		@Override
		protected void onPreExecute() {
			progressDialog.show();
		}

		@Override
		protected List<Post> doInBackground(Void... args) {
			List<Post> posts = app.getParser().retrieve();
			return posts;
		}

		@Override
		protected void onPostExecute(List<Post> taskPostList) {
			if (progressDialog.isShowing()) {
				progressDialog.dismiss();
			}
			if (!taskPostList.isEmpty()) {
				app.getPostList().clear();
				app.getPostList().addAll(taskPostList);
				resetListItems(taskPostList);

				// for the "reparse" button, we also need to reset the spinner
				// data and put selection on 0 (Daily Deals)
				// (and since we don't have a member variable for the
				// spinnerAdapter's data, we just reset one by one)
//				sectionSpinner.setSelection(0);
//				spinnerAdapter.clear();
//				spinnerAdapter.add("all");
//				spinnerAdapter.add("self");

			} else {
				Toast.makeText(PostList.this,
						getString(R.string.postlist_missing_data),
						Toast.LENGTH_LONG).show();
			}
		}
	}

	private class RetrieveImageTask extends AsyncTask<String, Void, Bitmap> {
		private ImageView imageView;

		public RetrieveImageTask(ImageView imageView) {
			this.imageView = imageView;
		}

		@Override
		protected Bitmap doInBackground(String... args) {
			Bitmap bitmap = app.retrieveBitmap(args[0]);
			return bitmap;
		}

		@Override
		protected void onPostExecute(Bitmap bitmap) {
			if (bitmap != null) {
				imageView.setImageBitmap(bitmap);
				app.getImageCache().put((Long) imageView.getTag(), bitmap);
				imageView.setTag(null);
			}
		}
	}
}